package com.mew.common.dc.redis;

import com.mew.common.enm.DataStateType;
import com.mew.common.entity.config.Game;
import com.mew.common.entity.config.Room;
import com.mew.common.entity.user.User;
import com.mew.common.exception.ResponseCode;
import com.mew.common.rest.bo.GameSession;
import com.zebra.common.OnlineType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class EnterRoomCheckService {

    @Autowired
    private UserProfileRedisService userProfileRedisService;
    @Autowired
    private GameRoomRedisService gameRoomRedisService;


    public ResponseCode checkEnterRoom(long userId, int gameId, int roomId, GameSession session){
        User user = userProfileRedisService.getRedisUserById(userId);
        if (null == user ){
            log.error("Enter check : user is null userId {}", userId);
            return ResponseCode.UserNotExist;
        }
        if (user.getState() == null || user.getState() == 0){
            return ResponseCode.UserState;
        }
        Game game =  gameRoomRedisService.getRedisGame(gameId);
        if (null == game){
            log.error("Enter check : game is null gameId {}", gameId);
            return ResponseCode.ParamInvalid;
        }

        Room room = gameRoomRedisService.getRedisRoom(gameId, roomId);
        if (null == room || room.getState() != DataStateType.Active.code()){
            log.error("Enter check : room is null roomdId", roomId);
            return ResponseCode.ParamInvalid;
        }


        if (game.getOnlineType() < OnlineType.Unbound.value() || (user.getWhiteList() != 1 && game.getOnlineType() < OnlineType.Online.value())){
            return ResponseCode.GameServerNotConfig;
        }

        session.setUserId(userId);
        session.setGid(user.getGid());
        session.setNickname(user.getNickname());
        session.setHeadPath(user.getHeadPath());

        session.setVfx(game.getGameVfx());
        session.setRoomBetArray(room.getBetArrs());

        session.setGameId(gameId);
        session.setRoomId(roomId);
        session.setWhiteList(user.getWhiteList());

        return ResponseCode.Success;

    }





}
